package com.hyd.project;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hyd.adapter.PurchaseInfoAdapter;
import com.hyd.application.MyApplication;
import com.hyd.service.MusicService;
import com.hyd.view.RippleView;

/**
 * 展示所有收货地址的界面，可以从中选择收货地址
 * 
 * @author llf
 * 
 */
public class ChoiceAddressActivity extends Activity implements
		SwipeRefreshLayout.OnRefreshListener {
	private SwipeRefreshLayout refreshLayout;
	private ListView myListView;
	private PurchaseInfoAdapter adapter;
	private ArrayList<String> dataList = new ArrayList<String>();
	private RelativeLayout footer;
	private Button add;
	private View priorView;
	private LayoutInflater inflater;

	private Handler hander = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				refreshLayout.setRefreshing(false);
			}
		};
	};

	// 播放器相关
	private RelativeLayout player_view;
	private ProgressBar progress;
	private Handler handler;
	private Runnable runnable;
	private ImageView play, loop;
	private RippleView playRipple, loopRipple, nextRipple, priorRipple;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choice_address);

		initView();
		initData();
		initClick();
	}

	private void initView() {
		refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);

		inflater = LayoutInflater.from(ChoiceAddressActivity.this);
		myListView = (ListView) findViewById(R.id.recieve_address_list);
		footer = (RelativeLayout) inflater.inflate(R.layout.purchase_footer,
				null);
		add = (Button) footer.findViewById(R.id.add);
		myListView.addFooterView(footer);

		// 播放器相关
		player_view = (RelativeLayout) findViewById(R.id.player_view);
		play = (ImageView) player_view.findViewById(R.id.play);
		loop = (ImageView) player_view.findViewById(R.id.loop);
		playRipple = (RippleView) player_view.findViewById(R.id.play_ripple);
		loopRipple = (RippleView) player_view.findViewById(R.id.loop_ripple);
		nextRipple = (RippleView) player_view.findViewById(R.id.next_ripple);
		priorRipple = (RippleView) player_view.findViewById(R.id.prior_ripple);
		progress = (ProgressBar) player_view.findViewById(R.id.seek);

		handler = new Handler();
		runnable = new Runnable() {

			@Override
			public void run() {
				if (MyApplication.playstatus == 1) {
					progress.setMax(MusicService.player.getDuration());
					progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				// 延迟启动线程，主要是为了保证启动线程的时候,播放音乐的服务已经启动
				handler.postDelayed(runnable, 50);
			}
		};

		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}

	private void initData() {
		for (int i = 0; i < 3; i++) {
			dataList.add(i + "");
		}
		adapter = new PurchaseInfoAdapter(ChoiceAddressActivity.this, dataList);
		myListView.setAdapter(adapter);
	}

	private void initClick() {
		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ChoiceAddressActivity.this,
						AppendAddressActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
			}
		});

		myListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (priorView != null) {
					priorView.setBackgroundResource(R.drawable.action_item_bg);
				}
				View child = view.findViewById(R.id.child);
				priorView = child;
				child.setBackgroundResource(R.drawable.orange_card_bg);
			}
		});

		playRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.playstatus == 0) {
					play.setBackgroundResource(R.drawable.player_hesitation);
					MyApplication.playstatus = 1;
					MusicService.goon();
					handler.post(runnable);
				} else {
					MusicService.pause();
					MyApplication.playstatus = 0;
					play.setBackgroundResource(R.drawable.play);

				}
			}
		});

		// 设置为循环播放
		loopRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.isLoop) {
					loop.setBackgroundResource(R.drawable.unloop);
					MusicService.player.setLooping(false);
					MyApplication.isLoop = false;
				} else {
					loop.setBackgroundResource(R.drawable.loop);
					MusicService.player.setLooping(true);
					MyApplication.isLoop = true;
				}
			}
		});

		nextRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playNext();
			}
		});

		priorRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playPrior();
			}
		});
	}

	public void complete(View v) {
		Toast.makeText(ChoiceAddressActivity.this, "选择地址完成返回",
				Toast.LENGTH_SHORT).show();
		finish();
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	public void back(View v) {
		finish();
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		hander.sendEmptyMessageDelayed(1, 2000);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(runnable);
		super.onDestroy();
	}
}
